package screen;

import resources.Assets;
import resources.IActivityRequestHandler;
import resources.Input;
import resources.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation;
import common.Constant;
import common.GameConfig;


public class MainMenuScreen extends Screen {
//	OrthographicCamera guiCam;
//	SpriteBatch batcher;
	
	private float stateTime;
	
	public  TextureRegion tetrisSlashRegion ;
	private Texture tetrisSlash;
	
	private int iTimer;
	private int playFrame;
	private int loadFrame;
	private int helpFrame;
	private int scoreFrame;
	private int quitFrame;
	
	public static boolean touchFacebook;
	public static boolean touchTwitter;
	
	public boolean flg = false;
	public boolean soundFlags = false;

	public  TextureRegion buttonTreeRegion;
	public  TextureRegion mainMenuRegion;
	public  TextureRegion titleRegion;
	public  TextureRegion chimbocauRegion;
	public  TextureRegion nguoilinhRegion;
	public  TextureRegion cotcoRegion;
	public  TextureRegion lacoCurrentFrame;
	public  TextureRegion maybayCurrentFrame;
	public  Texture mainButton;
	public  Texture playButton;
	public  Texture quitButton;
	public  Texture loadButton;
	public  Texture helpButton;
	public  Texture highScoreButton;
	public  Texture facebookTexture;
	public  Texture twitterTexture;
	public  Texture mainMenu;
	public  Texture buttonTree;
	public  Texture title;
	public  Texture chimbocau;
	public  Texture nguoilinh;
	public  Texture cotco;
	public  Texture laco;
	public  Texture soundOn;
	public  Texture soundOff;
	private Texture market;
	public  Texture maybayMainmenu2;
	
	public  TextureRegion buttonRegion [] = new TextureRegion[4];
	public  TextureRegion playButtonRegion [] = new TextureRegion[2] ;
	public  TextureRegion loadGameButtonRegion [] = new TextureRegion[2] ;
	public  TextureRegion hightscoreButtonRegion [] = new TextureRegion[2] ;
	public  TextureRegion helpButtonRegion [] = new TextureRegion[2] ;
	public  TextureRegion quitButtonRegion [] = new TextureRegion[2] ;
	public  TextureRegion facebookRegion [] = new TextureRegion[2] ;
	public  TextureRegion twitterRegion [] = new TextureRegion[2] ;	
	private TextureRegion marketRegion [] = new TextureRegion[2];
	public  TextureRegion lacoRegion [] = new TextureRegion[5];
	public  TextureRegion soundRegion [] = new TextureRegion[2];
	public  TextureRegion maybayMainRegion2 [] = new TextureRegion[7];
	
	public Animation lacoAnimation;
	public Animation maybayMainmenu2Animation;
	
	private boolean enableMarket = false;
	private int timeForButton;

	public MainMenuScreen (Game game) {
		super(game);
		this.game.setNumSreen(Constant.MainMenuScreen); 
		Gdx.input.setCatchBackKey(true);
		loadResource();
		
	}
	
	public MainMenuScreen(Game game, IActivityRequestHandler requestHandler) {
		super(game);
		this.game.setNumSreen(Constant.MainMenuScreen); 
		Gdx.input.setCatchBackKey(true);
//		System.out.println("chay");
		loadResource();
	}

	public void tick(Input input) {
		//System.out.println("Count: " + count);
				
		if (Assets.manager.update()){			
			if(input.buttons[Input.PLAYGAME]){
				System.out.print("Start new game\n");
				iTimer++;	
				if(iTimer % 5 == 0) playFrame++;
				if(playFrame > 3){
					iTimer = 0;
					playFrame = 0;
//					System.out.println("toi day chua?");
					GameScreen screen = new GameScreen(game);
					screen.initilize(Settings.level);
					this.game.setScreen(screen);
//					System.out.println("toi roi");
					input.releaseAllKeys();
					return;
				}	
			}
			else if(input.buttons[Input.LOADGAME]){
				iTimer++;	
				if(iTimer % 5 == 0) loadFrame++;
				if(loadFrame > 3){
					iTimer = 0;
					loadFrame = 0;
					
					input.releaseAllKeys();
					return;
				}	
			}
			else if(input.buttons[Input.HELPGAME]){
				
				iTimer++;	
				if(iTimer % 5 == 0) helpFrame++;
				if(helpFrame > 3){
					iTimer = 0;
					helpFrame = 0;
					
					input.releaseAllKeys();
					return;
				}	
			}
			else if(input.buttons[Input.HIGHSCORE]){
				
				iTimer++;	
				if(iTimer % 5 == 0) scoreFrame++;
				if(scoreFrame > 1){
					iTimer = 0;
					scoreFrame = 0;
					if(GameTetris.actionResolver != null)
					{
						GameTetris.actionResolver.boot();
					}
					
					input.releaseAllKeys();
					return;
				}	
			}
			else if(input.buttons[Input.EXITGAME]){
				System.out.print("exit game nak ");
				iTimer++;	
				if(iTimer % 5 == 0) quitFrame++;
				if(quitFrame > 3){
					iTimer = 0;
					quitFrame = 3;
					
					Gdx.app.exit();
					return;
				}				
			}			
		}	
	}
	
	@Override
	public void present () {
//		GLCommon gl = Gdx.gl;
//		gl.glClearColor(1, 0, 0, 1);
//		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		stateTime += Gdx.graphics.getDeltaTime();
		
		camera.update();
		spriteBatch.setProjectionMatrix(camera.combined);
		spriteBatch.begin();
		
		if (!Assets.manager.update()){
			spriteBatch.draw(tetrisSlashRegion, 0, 0);
		} else {					
			System.out.println("Giao dien man hinh chinh");
			spriteBatch.draw(tetrisSlashRegion, 0, 0);
			spriteBatch.draw(titleRegion, 50, 170);
			spriteBatch.draw(playFrame > 0 ? playButtonRegion[1] : playButtonRegion[0], 200, 400);
			spriteBatch.draw(loadFrame > 0 ? loadGameButtonRegion[1] : loadGameButtonRegion[0], 200, 300);
		}
		
		spriteBatch.end();


	}

	@Override
	public void pause () {
	
	}

	@Override
	public void resume () {
		System.out.println("Chris"+"resume() on mainmenu screen ===========================");
	}

	@Override
	public void dispose () {
		System.out.println("Chris"+" dispose () on mainmenu screen ===========================");
		unloadResource();
		spriteBatch.dispose();
	}
	private void loadResource(){
		stateTime = 0.0f;
		
		tetrisSlash = new Texture("data/Background.png");
		tetrisSlashRegion = new TextureRegion(tetrisSlash, 0, 0, 1024, 650);
		title = new Texture("data/tetrisTitle.png");
		titleRegion = new TextureRegion(title, 0, 0, 512, 512);
		playButton = new Texture("data/PlayButton.png");
		loadButton = new Texture("data/LoadGameButton.png");
		for(int i = 0; i < 2; i++){
			playButtonRegion[i] = new TextureRegion(playButton, 57 * i, 0, 57, 30); 
		}
		for(int i = 0; i < 2; i++){
			loadGameButtonRegion[i] = new TextureRegion(loadButton, 137 * i, 0, 137, 30); 
		}
	}
	private void unloadResource(){
		playButton.dispose();
		loadButton.dispose();
	}
}

